<!-- BEGIN PAGE HEADER-->
<?php $this->load->view('partials/breadcrumb.tpl') ?>
<!-- END PAGE HEADER-->
<!-- BEGIN PAGE CONTENT-->
<div class="row">
    <div class="col-xs-12 ">
        <!-- BEGIN SAMPLE FORM PORTLET-->
        <div class="portlet box green ">
            <?= edit_form_caption('添加入库记录') ?>
            <div class="portlet-body form">
                <?= form_open('', Array('class' => 'form-horizontal', 'role' => 'form', 'id' => 'form_edit', 'data-category-id' => $obj->id)) ?>
                <div class="form-body">
                    <?= edit_form_dropdown('shop_id', array('' => '选择账户组...') + $allShops, set_value('shop_id', $obj->shop_id), '选择账户组', "class='select2me' style='width:100%;'") ?>

                    <div class="form-group change-housing">
                        <label class="col-xs-3 control-label">货物批次</label>
                        <div class="col-xs-7">
                            <?php if ($thisMethod == 'edit'): ?>
                                <span id="warehousing" style="line-height: 28px" class="label label-danger"><?= $obj->warehousing ?></span>
                                <input type="hidden" name="warehousing" value="<?= $obj->warehousing ?>">
                            <?php else: ?>
                                <span id="warehousing" style="line-height: 28px" class="label label-danger"><?= $warehousing ?></span>
                                <input type="hidden" name="warehousing" value="<?= $warehousing ?>">
                                <span id="warehousing-merge" class="btn blue" style="float: right">选择汇入已有批次</span>
                            <?php endif; ?>

                        </div>
                    </div>

                    <div id="warehousing-time" style="display: none;">
                        <?= edit_form_dropdown('', array('' => '选择批次...'), set_value(''), '选择批次', "id='housing' class='select2me' style='width:100%;line-height:20px'") ?>
                    </div>

                    <?= edit_form_input('quantity', set_value('quantity', $obj->quantity), '商品数量') ?>
                    <?= edit_form_input('amount', set_value('amount', $obj->amount), '商品总价','required') ?>
                    <?= edit_form_input('related_person', set_value('related_person', $obj->related_person), '负责人') ?>
                    <input type="hidden" name="type" value="1">
                    <?= edit_form_dropdown('type_mark', array('' => '选择入库类型...') + IOSS_StaticConfig::getShippingInType(), set_value('type_mark', $obj->type_mark), '入库类型', "id='mine_se'") ?>
                    <?= edit_form_radio_list('status', MStorageRecord::getState(), set_value('status', ($thisMethod == 'edit' ? $obj->status : '1')), '入库状态') ?>
                    <div class="storage">
                        <?= edit_form_dropdown('location', array('' => '选择存储仓库...') + IOSS_StaticConfig::getShippingWarehouse(), set_value('location', $obj->location), '存储仓库', "id='storage'") ?>
                        <?= edit_form_dropdown('area', array('' => '选择仓库存储区...') + IOSS_StaticConfig::getShippingBlock(), set_value('area', $obj->area), '仓库存储区', "id='area'") ?>
                    </div>

                    <div id="order-no" style="display: none;">
                        <?= edit_form_input('order_no', set_value('order_no', $obj->order_no), '订单号（自发货）') ?>
                    </div>


                    <div class="form-group">
                        <label class="col-xs-3 control-label">采购时间</label>
                        <div class="col-xs-7">
                            <div class="input-group input-large date-picker input-daterange" data-date=""
                                 data-date-format="yyyy-mm-dd">
                                <input type="text" class="form-control" required name="purchase_time" value="<?= set_value('purchase_time', $obj->purchase_time) ?>" placeholder="采购时间">
                            </div>
                        </div>
                    </div>
                    <?= edit_form_textarea('remark', set_value('remark', $obj->remark), '备注信息') ?>
                </div>
                <div class="form-actions">
                    <div class="row">
                        <div class="col-xs-offset-3 col-xs-4">
                            <button type="button" id="add-in-submit" class="btn green">
                                <span class="md-click-circle md-click-animate"></span>保存
                            </button>
                            <a href="/storage/warehouse" class="btn default">返回</a>
                        </div>
                    </div>
                </div>
                <?= form_close() ?>
            </div>
        </div>
    </div>
</div>
<link href="<?= image_url('/static/assets/global/plugins/jstree/dist/themes/default/style.min.css') ?>" rel="stylesheet" type="text/css"/>
<script type="text/javascript">

    $("#add-in-submit").click(function () {
        if (!$("input[name='quantity']").val() || !$("input[name='amount']").val()){
            toastr.error('请输入数量及总价！');
            return false;
        }
        var p_time = $("input[name='purchase_time']").val();
        if (!p_time){
            toastr.error('请选择采购时间！');
            return false;
        }else {
            var storage = $("#storage");
            var area = $("#area");
            if (storage.is(':visible')){
                if (!storage.val() || !area.val()){
                    toastr.error('请选择存储仓库及存储位置！');
                    return false;
                }
            }
            $("#form_edit").submit();
        }
    });

    $(document).ready(function () {
        var initStatus = $('input[name="status"]:checked').val();
        if (initStatus == 2) {
            $('.storage').hide();
        }
        $("input[type='radio'][name='status']").change(function () {
            var value = $(this).val();
            if (value == 2) {
                $('.storage').fadeOut();
            } else {
                $('.storage').fadeIn();
            }
        });

        $("#warehousing-merge").click(function () {
            $.ajax({
                url: "<?= site_url('/storage/warehouse/ajaxWarehousing') ?>",
                method: 'POST',
                dataType: 'JSON',
                data: {'product_id': <?= $obj->product_id ?>},
                success: function (res) {
                    if (res.success) {
                        console.log($("select[id='housing']"));
                        $("#warehousing-time").toggle('fade');
                        var html = '';
                        for (var i = 0; i < res.data.length; i++) {
                            html += '<option value="' + res.data[i].warehousing + '">' + res.data[i].warehousing + '</option>'
                        }
                        $("select[id='housing']").append(html);

                    } else {
                        toastr.error('当前产品暂无入库批次记录！', '失败');
                        return false;
                    }
                },
                fail: function (res) {
                    console.log(res);
                }
            });
        });

        $("#housing").change(function () {
            if (!this.value){
                toastr.error('批次不可为空');
                return false;
            } else {
                $('.storage').fadeOut();
                $("#warehousing").html(this.value);
                $("input[name='warehousing']").val(this.value);
            }
        });

        $('#mine_se').change(function () {
            var choose_value = $('#mine_se').val();
            if (choose_value == 7) {
                $("#order-no").fadeIn();
            } else {
                $("#order-no").fadeOut();
            }
        });
    })
</script>
